<template>
  <div class="article-comments">
    <!-- 评论列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="item in list" :key="item.com_id">
        <van-image
          slot="icon"
          round
          width="30"
          height="30"
          style="margin-right: 10px;"
          :src="item.aut_photo"
        />
        <span style="color: #466b9d;" slot="title">{{ item.aut_name }}</span>
        <div slot="label">
          <p style="color: #363636;">{{ item.content }}</p>
          <p>
            <span style="margin-right: 10px;">{{
              item.pubdate | fromNow
            }}</span>
            <van-button @click="showFn(item)" size="mini" type="default"
              >回复（{{ item.reply_count }}）</van-button
            >
          </p>
        </div>
        <van-icon
          @click="likeFn(item)"
          slot="right-icon"
          :name="item.is_liking ? 'like' : 'like-o'"
        />
      </van-cell>
    </van-list>
    <!-- /评论列表 -->

    <!-- 发布评论 -->
    <van-cell-group class="publish-wrap">
      <van-field clearable placeholder="请输入评论内容" v-model.trim="content">
        <van-button @click="publishFn" slot="button" size="mini" type="info"
          >发布</van-button
        >
      </van-field>
    </van-cell-group>
    <!-- /发布评论 -->

    <!-- 评论回复组件 -->
    <van-action-sheet v-model="show" title="评论回复">
      <reply
        v-if="show"
        @addReplyCount="addReplyCountFn"
        :currentCommon="currentCommon"
        :artId="artId"
      ></reply>
    </van-action-sheet>
  </div>
</template>

<script>
import reply from './reply.vue'

import {
  getArticleCommon,
  addCommon,
  addCommonLike,
  removeCommonLike
} from '@/api/article'

export default {
  name: 'ArticleComment',
  data() {
    return {
      list: [], // 评论列表
      loading: false, // 上拉加载更多的 loading
      finished: false, // 是否加载结束
      offset: null,
      content: '',
      show: false,
      //   当前的评论消息 ==> 传递给子组件
      currentCommon: ''
    }
  },
  components: {
    reply
  },
  computed: {
    //   文章id
    artId() {
      return this.$route.params.id
    }
  },
  methods: {
    addReplyCountFn() {
      //   需要将当前评论的reply_count++
      this.currentCommon.reply_count++
    },
    showFn(item) {
      this.show = true

      // 把当前评论消息赋值给currentCommon
      this.currentCommon = item
    },
    async likeFn(item) {
      //   发送请求（点赞 && 取消点赞）
      //   addCommonLike()
      // removeCommonLike()
      //   console.log(item)
      if (item.is_liking) {
        // 点过赞 ==> removeCommonLike
        const res = await removeCommonLike(item.com_id)
        console.log(res)
      } else {
        const res = await addCommonLike(item.com_id)
        console.log(res)
      }

      // 提示
      this.$toast('操作成功')
      // 对item的is_liking置反
      item.is_liking = !item.is_liking
    },
    async publishFn() {
      // 优化 没有内容就不进行发布
      if (!this.content) {
        return this.$toast('请输入评论内容')
      }

      //   获取到输入框的内容 ==> this.content ==> 发送ajax请求

      const res = await addCommon(this.artId, this.content)
      //   console.log(res)

      // 1. 把服务器给的new_obj 添加到list数组 ==> unshift
      // 2. 提示
      // 3. 清空输入框内容

      // 1.
      this.list.unshift(res.data.new_obj)

      //   2.
      this.$toast('发布成功')

      //   3.
      this.content = ''
    },
    async onLoad() {
      // 异步更新数据
      const res = await getArticleCommon(this.artId, this.offset)
      console.log(res)

      this.list = [...this.list, ...res.data.results]

      // 表示加载结束了
      this.loading = false

      // 处理offset
      this.offset = res.data.last_id

      // 处理finished
      if (res.data.results.length === 0) {
        //   没有更多数据
        this.finished = true
      }
    }
  }
}
</script>

<style scoped lang="less">
// 发表评论的区域是固定在下端的
.publish-wrap {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
}
// 给发表评论区空出地方
.van-list {
  margin-bottom: 45px;
}
</style>
